unit testcriteria;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  fpcunit,
  common, //include common
  testregistry,
  hadecriteria;

type

  { TTestCriteria }

  TTestCriteria = class(TTestCase)
  private
    FCriteria: IHadeExpression;
    fPerson:TPerson;
  protected
    procedure SetUp;override;
    procedure TearDown;override;
  published
    procedure TestEqualStringValue;
    procedure TestEqualNumericValue;
    procedure TestEqualDate;
    procedure TestLessThan;
    procedure TestGreaterThan;
    procedure TestIn;
    procedure TestNotIn;
    procedure TestLike;
    procedure TestNotLike;
    procedure TestBetWeen;
    procedure testNotBetween;

    procedure TestCriteria;
  end;

implementation

procedure TTestCriteria.SetUp;
begin
  fPerson:=TPerson.create;
end;

procedure TTestCriteria.TearDown;
begin
  freeAndNil(fPerson);
end;

procedure TTestCriteria.TestEqualStringValue;
begin
  FCriteria := THadeEqual.Create('First_name', 'Fajar');
  self.AssertEquals(Trim(' First_name = ''Fajar'' '), Trim(FCriteria.getClause));

  FCriteria := THadeNotEqual.Create('First_name', 'Fajar');
  self.AssertEquals(Trim(' First_name <> ''Fajar'' '), Trim(FCriteria.getClause));
end;

procedure TTestCriteria.TestEqualNumericValue;
begin
  FCriteria := THadeEqual.Create('Age', 21);
  self.AssertEquals(Trim(' Age = 21 '), Trim(FCriteria.getClause));
end;

procedure TTestCriteria.TestEqualDate;
begin
  FCriteria := THadeEqual.Create('reg_date', date);
  self.AssertEquals(Trim(' reg_date = ' + datetostr(date)), Trim(FCriteria.getClause));
end;

procedure TTestCriteria.TestLessThan;
begin
  FCriteria := THadeLessThan.Create('Age', 18);
  AssertEquals('Age < 18', FCriteria.getClause);

  FCriteria := THadeLessAndEqualThan.Create('Age', 18);
  AssertEquals('Age <= 18', FCriteria.getClause);
end;

procedure TTestCriteria.TestGreaterThan;
begin
  FCriteria := THadeGreaterThan.Create('Age', 18);
  AssertEquals('Age > 18', FCriteria.getClause);

  FCriteria := THadeGreaterAndEqualThan.Create('Age', 18);
  AssertEquals('Age >= 18', FCriteria.getClause);
end;

procedure TTestCriteria.TestIn;
begin
  FCriteria := THadeIn.Create('Age', [21,18]);
  AssertEquals('Age IN (21,18)', FCriteria.getClause);
end;

procedure TTestCriteria.TestNotIn;
begin
  FCriteria := THadeNotIn.Create('Age', [21,18]);
  AssertEquals('Age NOT IN (21,18)', FCriteria.getClause);
end;

procedure TTestCriteria.TestLike;
begin
  FCriteria := THadeLike.Create('first_name','Tokay');
  AssertEquals('first_name LIKE ''Tokay''', FCriteria.getClause);
end;

procedure TTestCriteria.TestNotLike;
begin
  FCriteria := THadeNotLike.Create('first_name','Tokay');
  AssertEquals('first_name NOT LIKE ''Tokay''', FCriteria.getClause);
end;

procedure TTestCriteria.TestBetWeen;
begin
  FCriteria := THadeBetween.Create('age',18,30);
  AssertEquals('age BETWEEN 18 AND 30', FCriteria.getClause);
end;

procedure TTestCriteria.testNotBetween;
begin
  FCriteria := THadeNotBetween.Create('age',18,30);
  AssertEquals('age NOT BETWEEN 18 AND 30', FCriteria.getClause);
end;

procedure TTestCriteria.TestCriteria;
var
  FExpr: THadeCriteria;
  sExpect: string;
  FExpr1: THadeCriteria;
  sExpect1: String;
begin
  FExpr := THadeCriteria.Create(fPerson,'test');
  Try
    FExpr.Add_Or();//it should do nothing,see implementation for details
    FExpr.Equal('First_Name', 'Fajar');
    FExpr.notEqual('Age',35);
    FExpr.LessThan('Age', 30);
    FExpr.notIn('Last_Name',['bau']);

    sExpect :=
      'First_Name = ''Fajar'' AND Age <> 35 AND Age < 30 AND '+
      'last_name NOT IN (''bau'')';
    AssertEquals(UpperCase(Trim(sExpect)),upperCase(Trim(FExpr.getClause)));


    FExpr1 := THadeCriteria.Create(fPerson,'test');
    FExpr1.Like('First_Name','Tokay');
    FExpr1.Add_Or();
    FExpr1.NotLike('First_Name','Fajar');
    FExpr1.Between('age',18,30);

    sExpect1 := 'first_name LIKE ''Tokay'' OR first_name NOT LIKE ''Fajar'' AND age BETWEEN 18 AND 30';
    AssertEquals(UpperCase(Trim(sExpect1)),upperCase(Trim(FExpr1.getClause)));
  finally
    FreeAndNil(fExpr);
    FreeAndNil(fExpr1);
  end;
end;

initialization

  RegisterTest(TTestCriteria);
end.

